package com.weipai.springboot.base.interceptor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weipai.springboot.model.sys.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.NamedThreadLocal;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 这个拦截器拦截所有的请求，主要 的作用就是现实每个接口的请求所耗费的时间
 */
public class ExcuteTimeInterceptor extends HandlerInterceptorAdapter {
	final static Logger log = LoggerFactory
			.getLogger(ExcuteTimeInterceptor.class);
	
	private NamedThreadLocal<Date>  startTimeThreadLocal = new NamedThreadLocal<Date>("StopWatch-StartTime");  
	private static DateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss:SSS");

	@Override
	public boolean preHandle(HttpServletRequest rquest, HttpServletResponse response,
			Object obj) throws Exception {
		String originHeader=rquest.getHeader("Origin");
		if(null!=originHeader){
			String []  allowDomain= {"http://127.0.0.1:8020","http://192.168.1.147:8020","http://gld.gonglaoda.cn","http://192.168.1.60:8080",
					"http://192.168.1.106:8080","http://192.168.1.106:8080","http://192.168.1.106", "http://192.168.1.106:8020", "http://192.168.1.60:8020","http://192.168.1.60:8080","http://www.gonglaoda.cn","http://www.hbgzyc.com","http://192.168.1.106:8081"
					,"http://192.168.1.126:8080"	,"http://192.168.1.126:8081","http://192.168.1.126","220.181.132.235","182.118.31.216"};
			Set allowedOrigins= new HashSet(Arrays.asList(allowDomain));
			/*if (allowedOrigins.contains(originHeader)){*/
				response.setHeader("Access-Control-Allow-Origin", originHeader);
				response.setHeader("Access-Control-Expose-Headers", User.HEADER_TYPE_KEY);
				response.setHeader("Access-Control-Allow-Credentials", "true");
				response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,DELETE,PUT,DELETE");
				response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, user-type, Authentication-Token");
				startTimeThreadLocal.set(new Date());//线程绑定变量（该数据只有当前请求的线程可见）
				return true;//继续流程  
			/*}*/
		}else{
			startTimeThreadLocal.set(new Date());//线程绑定变量（该数据只有当前请求的线程可见）
			return true;
		}
		/*return false;*/
		/*String originHeader=rquest.getHeader("Origin");
		if(null!=originHeader){
			response.setHeader("Access-Control-Allow-Origin", originHeader);
			response.setHeader("Access-Control-Expose-Headers", User.HEADER_TYPE_KEY);
			response.setHeader("Access-Control-Allow-Credentials", "true");
			response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,DELETE,PUT,DELETE");
			response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, user-type");
			startTimeThreadLocal.set(new Date());//线程绑定变量（该数据只有当前请求的线程可见）
			return true;//继续流程  
		}else{
			startTimeThreadLocal.set(new Date());//线程绑定变量（该数据只有当前请求的线程可见）
			return true;
		}*/
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception exception)
			throws Exception {
		Date endTime = new Date();//2、结束时间  
        Date beginTime = startTimeThreadLocal.get();//得到线程绑定的局部变量（开始时间）  
        long consumeTime = endTime.getTime() - beginTime.getTime();//3、消耗的时间
        log.info("请求:" + request.getRequestURI() + "，请求处理开始时间：" + sdf.format(beginTime) + "，请求处理结束时间：" + sdf.format(endTime)
				+ "，耗时：" + consumeTime + "毫秒");
	}
}
